package com.jcnet.bizaia;
import java.text.BreakIterator;
import java.text.CharacterIterator;
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jcnet.bizaia.adapter.MainMenuAdapter;
import com.jcnet.bizaia.bean.SeVoiceList;
import com.jcnet.bizaia.bean.VoiceDetailBean;
import com.jcnet.bizaia.fragment.DiscussionInfoFragment;
import com.jcnet.bizaia.fragment.DownLoadFragment;
import com.jcnet.bizaia.fragment.FavouriteDiscountFragment;
import com.jcnet.bizaia.fragment.FavouriteVodFragment;
import com.jcnet.bizaia.fragment.FavouriteVoiceFragment;
import com.jcnet.bizaia.fragment.MusicMediaControllerFragment;
import com.jcnet.bizaia.fragment.SettingTalkVodFragment;
import com.jcnet.bizaia.fragment.SettingTalkVoiceFragment;
import com.jcnet.bizaia.fragment.VodInfoFragment;
import com.jcnet.bizaia.fragment.VoiceInfoFragment;
import com.mozillaonline.providers.DownloadManager;
import com.mozillaonline.providers.downloads.ui.CopyOfDownloadList;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

public class MyJCDownloadActivity  extends JCSimpleSherlockActivity {
	
	
	private TabPageIndicator indicator;
	
	private ViewPager viewPager;
	
	private String [] mainTitles; 
	
	private List<Fragment> fragments;
	
	private MusicMediaControllerFragment mediaControllerFragment;
	
	private BroadcastReceiver broadcastReceiver;
	
	public static final String PLAY_LOCAL_MUSIC = "PLAY_LOCAL_MUSIC";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourite_with_music);
		super.jcTitle = getString(R.string.setting);
		super.jcSubtitle =getString(R.string.download);
		super.initActionBar();
		mainTitles = getResources().getStringArray(R.array.downloadMenu);
		initView();
		mediaControllerFragment = (MusicMediaControllerFragment) getSupportFragmentManager().findFragmentById(R.id.musicColl);
		getSupportFragmentManager().beginTransaction().hide(mediaControllerFragment).commit();
		
		broadcastReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
				System.out.println("mainInfoBeans.size())))))="+App.seVoiceList.getMainInfoBeans().size());
				System.out.println(App.seVoiceList.getMainInfoBeans().get(0).getVideourl());
				System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
				
				mediaControllerFragment.musicPlayerService.setMusicList(App.seVoiceList.getMainInfoBeans(),intent.getIntExtra("pos", 0));
				//mediaControllerFragment.musicPlayerService.play();
			}
		};
		
		IntentFilter intentFilter = new IntentFilter(PLAY_LOCAL_MUSIC);
		
		registerReceiver(broadcastReceiver, intentFilter);
		
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(broadcastReceiver);
	}
	

	private void initView() {
		// TODO Auto-generated method stub
		initData();
		
		indicator = (TabPageIndicator)findViewById(R.id.indicator);
		viewPager = (ViewPager)findViewById(R.id.pager);
		viewPager.setAdapter(new GoogleMusicAdapter(getSupportFragmentManager()));
		viewPager.setOffscreenPageLimit(mainTitles.length);
		viewPager.setCurrentItem(0);
		indicator.setViewPager(viewPager);
		
		
		
		indicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				System.out.println("onPageSelected______________________"+arg0);
				if(arg0==0){
					getSupportFragmentManager().beginTransaction().hide(mediaControllerFragment).commit();
				}else{
					getSupportFragmentManager().beginTransaction().show(mediaControllerFragment).commit();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	private void initData() {
		// TODO Auto-generated method stub
		fragments = new ArrayList<Fragment>();
		fragments.add(new DownLoadFragment(1));
		fragments.add(new DownLoadFragment(2));
		fragments.add(new CopyOfDownloadList());
		  
	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	
	
	   class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
	        public GoogleMusicAdapter(FragmentManager fm) {
	            super(fm);
	        }

	        @Override
	        public Fragment getItem(int position) {
	        	
	        	return fragments.get(position);
	            //return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
	        }

	        @Override
	        public CharSequence getPageTitle(int position) {
	            return mainTitles[position];
	        }

	        @Override public int getIconResId(int index) {
	          return 0;
	        }

	      @Override
	        public int getCount() {
	          return mainTitles.length;
	        }
	    }
	



	
}
